﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using AC;

#if UNITY_EDITOR
using UnityEditor;
#endif

[System.Serializable]
public class ActionDisableAll : Action {

	public GameObject[] allHotspots;
	public GameObject ACObj;
	public enum Changetype { Enable, Disable};
	public Changetype changeButton = Changetype.Enable;

	public ActionDisableAll(){
		this.isDisplayed = true;
		title = "Hotspot: Enable All";
	}


	void Awake(){
		allHotspots = GameObject.FindGameObjectsWithTag ("Hotspot");
	}

	#if UNITY_EDITOR

	override public void ShowGUI(){
		ACObj = (GameObject)EditorGUILayout.ObjectField ("Object to change layer: ", ACObj, typeof(GameObject), true);
		changeButton = (Changetype)EditorGUILayout.EnumPopup ("Change to all Hotspots: ", changeButton);

		AfterRunningOption ();
	}

	#endif

	
	override public float Run(){
		SetLayerRecursively (ACObj, 2);

		if (changeButton == Changetype.Enable)
			SetLayerRecursively (ACObj, 0);
		if (changeButton == Changetype.Disable)
			SetLayerRecursively (ACObj, 2);
		
		return 0.0f;
	}

	void SetLayerRecursively(GameObject obj, int newLayer)
	{
		if (null == obj)
		{
			return;
		}
		
		obj.layer = newLayer;
		
		foreach (Transform child in obj.transform)
		{
			if (null == child)
			{
				continue;
			}
			SetLayerRecursively(child.gameObject, newLayer);
		}
	}
}
